﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using CommandMgr.Entities;

namespace CommandMgr.Repository
{
    public class Repository
    {
        CommandContext _context;

        public Repository(CommandContext context)
        {
            _context = context;
        }

        public void AddCommand(string Name, string Descr)
        {
            Command command = new Command 
                { 
                    Name = Name, 
                    Descr = Descr,
                    LastUpdated = DateTime.Now,
                    Status = Command.CommandStatus.idle
                };
            _context.Commands.Add(command);
            _context.SaveChanges();
        }

        public Command GetCommand(string Name)
        {
            return (from c in _context.Commands where c.Name == Name select c).FirstOrDefault();
        }

        public List<Command> ListCommands()
        {
            return (from c in _context.Commands select c).ToList();
        }

        public List<string> ListCommandNames()
        {
            var results = (from c in _context.Commands select(new { c.Name })).ToList();
            List<string> nameList = new List<string>();
            foreach (var result in results)
                nameList.Add(result.Name);
            return nameList;
        }

        public void UpdateCommand(Command command)
        {
            command.LastUpdated = DateTime.Now;
            _context.Commands.Attach(command);
            _context.Entry(command).State = EntityState.Modified;
            _context.SaveChanges();
        }

        public void AddCommandToQueue(string Name)
        {
            Command command = GetCommand(Name);
            if (command == null)
                throw new Exception("Not a valid command");

            if (command.Status == Command.CommandStatus.queued)
                throw new Exception("Command already queued");

            command.Status = Command.CommandStatus.queued;
            UpdateCommand(command);

        }

        public bool RemoveCommandFromQueue(string Name)
        {
            bool foundIt = false;

            Command qItem = (from q in _context.Commands where q.Name == Name && q.StatusCode == (byte)Command.CommandStatus.queued select q).FirstOrDefault();
            if (qItem != null)
            {
                foundIt = true;
                qItem.Status = Command.CommandStatus.dequeued;
                UpdateCommand(qItem);
            }

            return foundIt;
        }

        public void UpdateCommandStatus(string Name, Command.CommandStatus Status, string Response = "")
        {
            Command command = GetCommand(Name);
            if (command == null)
                throw new Exception("Not a valid command");
            command.Status = Command.CommandStatus.completed;
            command.Response = Response;
            UpdateCommand(command);
        }

        public void DeleteCommand(string Name)
        {
            Command command = new Command {Name = Name};
            _context.Commands.Attach(command);
            _context.Commands.Remove(command);
            _context.SaveChanges();
        }
 
    }
}
